package com.example.draw;

import com.example.view.DisplayJPanel;

import java.awt.*;
/**
 * @author:我叫程晓非
 * @desc:Rect绘图类
 * @date:2021-11-07
 * @version:1.0
 */
public class Rect {
    public static final int MAX_WIDTH = 80;
    public static final int MAX_HEIGHT = 80;
    public static final int MIN_WIDTH = 10;
    public static final int MIN_HEIGHT = 10;
    int x;
    int y;
    int width;
    int height;
    Color color;



    public void draw(Graphics g){
        g.setColor(color);
        g.drawRect(x,y,width,height);
        g.drawLine(x,y,x+width,y+height);
        g.drawLine(x+width,y,x,y+height);
    }

    public Rect(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public void setX(int x,int imgX,int imgWidth) {
        if(x > imgX && x <= imgX+imgWidth - this.width){
            this.x = x;
        }

    }
    public int getY() {
        return y;
    }

    public void setY(int y,int imgY,int imgHeight) {
        if(y >= imgY && y <= imgY+imgHeight - this.height){
            this.y = y;
        }
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        if(width >= MIN_WIDTH && width<=MAX_WIDTH ){
            this.width = width;
        }

    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {

        if(height >= MIN_HEIGHT && height<=MAX_HEIGHT){
            this.height = height;
        }
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
    //根据缩放参数获取手机相应坐标
    public Point getCenterPoint(int imgX,int imgY,float scale){
        Point result = new Point(x+width/2,y+height/2);
        //获取在图片上的相对坐标
        result.x -= imgX;
        result.y -= imgY;
        //获取缩放前坐标
        result.x = (int)(result.x*scale);
        result.y = (int)(result.y*scale);

        return result;
    }
}
